package com.luck.easyfoodbatch.fragment;

import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.MineGridAdapter;
import com.luck.easyfoodbatch.db.MessageDB;
import com.luck.easyfoodbatch.dialog.CustomDialog;
import com.luck.easyfoodbatch.entity.MineGrid;
import com.luck.easyfoodbatch.entity.User;
import com.luck.easyfoodbatch.glide.GlideCircleTransform;
import com.luck.easyfoodbatch.mvp.mine.DaySignModel;
import com.luck.easyfoodbatch.mvp.mine.DaySignPresenter;
import com.luck.easyfoodbatch.mvp.mine.DaySignView;
import com.luck.easyfoodbatch.mvp.mine.MemberInfoModel;
import com.luck.easyfoodbatch.mvp.mine.MemberInfoPresenter;
import com.luck.easyfoodbatch.mvp.mine.MemberInfoView;
import com.luck.easyfoodbatch.mvp.model.SignModel;
import com.luck.easyfoodbatch.mvp.model.UserModel;
import com.luck.easyfoodbatch.runtimepermissions.PermissionsManager;
import com.luck.easyfoodbatch.ui.AddressManagementActivity;
import com.luck.easyfoodbatch.ui.AnnualFeeActivity;
import com.luck.easyfoodbatch.ui.MessageActivity;
import com.luck.easyfoodbatch.ui.MyCollectionActivity;
import com.luck.easyfoodbatch.ui.MyCouponActivity;
import com.luck.easyfoodbatch.ui.MyOrderActivity;
import com.luck.easyfoodbatch.ui.MyPointsActivity;
import com.luck.easyfoodbatch.ui.PersonalActivity;
import com.luck.easyfoodbatch.ui.ReturnActivity;
import com.luck.easyfoodbatch.ui.SettingActivity;
import com.luck.easyfoodbatch.ui.ShopInfoDetailActivity;
import com.luck.easyfoodbatch.ui.ShopInfoT1Activity;
import com.luck.easyfoodbatch.ui.ShopManagementActivity;
import com.luck.easyfoodbatch.util.FileUtils;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import com.mob.tools.utils.UIHandler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2017/4/5.
 */

public class MineFragment extends BaseFragment implements DaySignView, MemberInfoView, PlatformActionListener, Handler.Callback, View.OnClickListener {
    private LinearLayout ll_right_sign;
    private LinearLayout ll_msg;
    private ImageView iv_mine_msg;
    private com.luck.easyfoodbatch.custom.RoundedImageView iv_icon;
    private ImageView iv_vip;
    private TextView tv_name;
    private TextView tv_info;
    private TextView tv_mine_integral;
    private TextView tv_mine_coupon;
    private TextView tv_mine_collect;
    private TextView tv_mine_share;
    private RelativeLayout rl_my_order;
    private LinearLayout ll_payment;
    private TextView tv_wait_pay_num;
    private LinearLayout ll_shipped;
    private TextView tv_wait_delivery_num;
    private LinearLayout ll_receipt;
    private TextView tv_wait_receipt_num;
    private LinearLayout ll_evaluated;
    private TextView tv_wait_comment_num;
    private LinearLayout ll_return;
    private TextView tv_wait_return_num;
    private TextView tv_mine_Business_info;
    private TextView tv_mine_shop;
    private TextView tv_mine_address;
    private TextView tv_mine_settings;
    private View rootView;
    private MineGridAdapter adapter;
    private List<MineGrid> grids = new ArrayList<>();
    private MineGrid mineGrid;
    private DaySignModel daySignModel;
    private MemberInfoModel memberInfoModel;
    private static final int MSG_ACTION_CCALLBACK = 2;
    private String share_title = "易食批", share_text = "这么专业的食品批发平台，好伙伴必须通知你！快来下载易食批app吧！钱赚更多也就是动动手指的事儿！", share_url = "";
    private MessageDB messageDB;
    // 1是客服权限只能访问我的商品和消息模块（商品和聊天）
    // 2是运营，只能访问我的商品模块（管理商品
    // 3是财务，只能访问商家信息模块（支付） ,也有可能同时兼备多个权限，（1,2就代表既是客服也是运营）
    private String role = "0";
    private final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 1013;
    private Handler mHander = new Handler();
    private List<Integer> roles = new ArrayList<>();// 权限标识

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_mine, container, false);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            View mStateBarFixer = rootView.findViewById(R.id.status_bar_fix);
            mStateBarFixer.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(getActivity())));
            mStateBarFixer.setBackgroundColor(getResources().getColor(R.color.black));
            mHander.postDelayed(new Runnable() {
                @Override
                public void run() {
                    inputImageSdcard();
                }
            }, 3000);
            initViews();
        }
        findViewById(rootView);
        setListener();
        return rootView;
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 设置个人信息数据
     */
    protected void initData() {
        if (isCheckedLogin()) {
            int msgCountTotal = getUnreadMsgCountTotal();
            int messagesCount = messageDB.getByAllMessagesCount();
            if (messagesCount > 0 || msgCountTotal > 0) {
                iv_mine_msg.setImageResource(R.mipmap.home_xiaoxi_01_1x);
            } else {
                iv_mine_msg.setImageResource(R.mipmap.home_xiaoxi_00_1x);
            }
            memberInfoModel.getMemberInfo();
            user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
            if (user != null) {
                share_url = "http://yishipi.com.cn/yspShare/share.html?uid=" + user.getUid();
                int is_year_fee = user.getIs_year_fee();
                role = user.getRole();
                roles.clear();
                String[] split = role.split(",");
                for (String code : split) {
                    if (!Tools.isNull(code)) {
                        roles.add(Integer.parseInt(code));
                    }
                }
                switch (is_year_fee) {
                    case 0:
                        // 未交年费
                        iv_vip.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        // 已交年费
                        iv_vip.setVisibility(View.VISIBLE);
                        break;
                }
                if (!Tools.isNull(user.getNickname())) {
                    tv_name.setText(user.getNickname());
                } else {
                    tv_name.setText(user.getMobile());
                }
                String path = HttpUtil.IMG_BASE_URL + user.getAvatar_path();
                if (!Tools.isNull(user.getAvatar_path())) {
                    Glide.with(getActivity())
                            .load(path)
                            .centerCrop()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .transform(new GlideCircleTransform(getActivity()))
                            .into(iv_icon);
                } else {
                    iv_icon.setImageResource(R.mipmap.kong);
                }
                int wait_pay_num = user.getWait_pay_num();
                int wait_delivery_num = user.getWait_delivery_num();
                int wait_receipt_num = user.getWait_receipt_num();
                int wait_comment_num = user.getWait_comment_num();
                int wait_return_num = user.getRefund_num();
                // 待付款
                if (wait_pay_num > 0) {
                    tv_wait_pay_num.setVisibility(View.VISIBLE);
                    tv_wait_pay_num.setText(wait_pay_num + "");
                } else {
                    tv_wait_pay_num.setVisibility(View.GONE);
                }
                // 待发货
                if (wait_delivery_num > 0) {
                    tv_wait_delivery_num.setVisibility(View.VISIBLE);
                    tv_wait_delivery_num.setText(wait_delivery_num + "");
                } else {
                    tv_wait_delivery_num.setVisibility(View.GONE);
                }
                // 待收货
                if (wait_receipt_num > 0) {
                    tv_wait_receipt_num.setVisibility(View.VISIBLE);
                    tv_wait_receipt_num.setText(wait_receipt_num + "");
                } else {
                    tv_wait_receipt_num.setVisibility(View.GONE);
                }
                // 待评价
                if (wait_comment_num > 0) {
                    tv_wait_comment_num.setVisibility(View.VISIBLE);
                    tv_wait_comment_num.setText(wait_comment_num + "");
                } else {
                    tv_wait_comment_num.setVisibility(View.GONE);
                }
                // 退换货
                if (wait_return_num > 0) {
                    tv_wait_return_num.setVisibility(View.VISIBLE);
                    tv_wait_return_num.setText(wait_return_num + "");
                } else {
                    tv_wait_return_num.setVisibility(View.GONE);
                }

            }
        }
    }

    /**
     * get unread message count
     * 获取聊天未读消息数量
     *
     * @return
     */
    public int getUnreadMsgCountTotal() {
        int unreadMsgCountTotal = 0;
        int chatroomUnreadMsgCount = 0;
        unreadMsgCountTotal = EMClient.getInstance().chatManager().getUnreadMsgsCount();
        for (EMConversation conversation : EMClient.getInstance().chatManager().getAllConversations().values()) {
            if (conversation.getType() == EMConversation.EMConversationType.ChatRoom)
                chatroomUnreadMsgCount = chatroomUnreadMsgCount + conversation.getUnreadMsgCount();
        }
        return unreadMsgCountTotal - chatroomUnreadMsgCount;
    }

    /**
     * 设置新消息提醒，聊天或系统消息
     */
    public void initMessage() {
        if (iv_mine_msg != null)
            if (isCheckedLogin()) {
                int msgCountTotal = getUnreadMsgCountTotal();
                int messagesCount = messageDB.getByAllMessagesCount();
                if (messagesCount > 0 || msgCountTotal > 0) {
                    iv_mine_msg.setImageResource(R.mipmap.home_xiaoxi_01_1x);
                } else {
                    iv_mine_msg.setImageResource(R.mipmap.home_xiaoxi_01_1x);
                }
            }
    }

    private void initViews() {
        messageDB = new MessageDB(getActivity());
        memberInfoModel = new MemberInfoPresenter(this, mContext);
    }

    private void requestPermission(final String permission, String rationale, final int requestCode) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)) {
            new AlertDialog.Builder(getActivity())
                    .setTitle("读取sdcard权限")
                    .setMessage(rationale)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode);
                        }
                    })
                    .setNegativeButton("取消", null)
                    .create().show();
        } else {
            ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                inputImageSdcard();

            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }

    /**
     * 写入图片到内存卡
     */
    private void inputImageSdcard() {

        try {
            InputStream is = getActivity().getAssets().open("logo.png");

            File file = new File(FileUtils.IMAGE_PATH + "logo.png");// 新建一个file文件
            if (!file.exists()) {

            } else {
                return;
            }
            FileOutputStream fos = new FileOutputStream(file);// 对应文件建立输出流
            byte[] buffer = new byte[1024];// 新建缓存 用来存储 从网络读取数据 再写入文件
            int len = 0;
            while ((len = is.read(buffer)) != -1) {// 当没有读到最后的时候
                fos.write(buffer, 0, len);// 将缓存中的存储的文件流秀娥问file文件
            }
            fos.flush();// 将缓存中的写入file
            fos.close();
            is.close();// 将输入流 输出流关闭
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 签到
     */
    private void showSignDialog(int type, String msg) {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth * 3 / 4,
                App.screenHeight / 4, R.layout.wind_sign_dialog_xml, R.style.Theme_dialog);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        switch (type) {
            case 0:
                tv_title.setText(msg);
                tv_content.setText("明天请记得保持哦");
                break;
            case 1:
                tv_title.setText("签到成功！");
                tv_content.setText(msg);
                break;
        }

        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 分享
     */
    private void showShareDialog() {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth,
                App.screenHeight, R.layout.wind_share_dialog, R.style.Theme_dialog);
        ImageView iv_close = (ImageView) dialog.findViewById(R.id.iv_close);
        TextView tv_invite_code = (TextView) dialog.findViewById(R.id.tv_invite_code);
        ImageView iv_share_wechat = (ImageView) dialog.findViewById(R.id.iv_share_wechat);
        ImageView iv_share_pyq = (ImageView) dialog.findViewById(R.id.iv_share_pyq);
        ImageView iv_share_qq = (ImageView) dialog.findViewById(R.id.iv_share_qq);
        tv_invite_code.setText("邀请码：" + user.getInvite_code());
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        iv_share_wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Tools.isWeixinAvilible(getActivity())) {
                    T.showToast(mContext, "正在启动微信，请稍候...");
                    weChat();
                } else {
                    T.showToast(mContext, "请安装微信客户端~");
                }
                dialog.dismiss();
            }
        });
        iv_share_pyq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Tools.isWeixinAvilible(getActivity())) {
                    T.showToast(mContext, "正在启动微信，请稍候...");
                    weChatMoments();
                } else {
                    T.showToast(mContext, "请安装微信客户端~");
                }
                dialog.dismiss();
            }
        });
        iv_share_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                T.showToast(mContext, "正在启动QQ，请稍候...");
                qq();
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 分享微信
     */
    private void weChat() {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(share_title);
        sp.setText(share_text);
        sp.setImagePath(FileUtils.IMAGE_PATH + "logo.png");
        sp.setShareType(Platform.SHARE_TEXT);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setUrl(share_url);
        Platform qq = ShareSDK.getPlatform(Wechat.NAME);
        qq.setPlatformActionListener(this);
        qq.share(sp);
    }

    /**
     * 分享微信朋友圈
     */
    private void weChatMoments() {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(share_title);
        sp.setText(share_text);
        sp.setUrl(share_url);
        sp.setImagePath(FileUtils.IMAGE_PATH + "logo.png");
        sp.setShareType(Platform.SHARE_TEXT);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        Platform qq = ShareSDK.getPlatform(WechatMoments.NAME);
        qq.setPlatformActionListener(this);
        qq.share(sp);
    }

    /**
     * 分享QQ
     */
    private void qq() {
        Platform.ShareParams shareParams = new Platform.ShareParams();
        shareParams.setTitle(share_title);
        shareParams.setTitleUrl(share_url); // 标题的超链接
        shareParams.setText(share_text);
        shareParams.setComment("我对此分享内容的评论");
        shareParams.setSite(getString(R.string.app_name));
        shareParams.setImagePath(FileUtils.IMAGE_PATH + "logo.png");
        shareParams.setSiteUrl(share_url);
        Platform qq = ShareSDK.getPlatform(mContext, QQ.NAME);
        qq.setPlatformActionListener(this);
        qq.share(shareParams);
    }


    private void findViewById(View view) {
        ll_right_sign = (LinearLayout) view.findViewById(R.id.ll_right_sign);
        ll_msg = (LinearLayout) view.findViewById(R.id.ll_msg);
        iv_mine_msg = (ImageView) view.findViewById(R.id.iv_mine_msg);
        iv_icon = (com.luck.easyfoodbatch.custom.RoundedImageView) view.findViewById(R.id.iv_icon);
        iv_vip = (ImageView) view.findViewById(R.id.iv_vip);
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_info = (TextView) view.findViewById(R.id.tv_info);
        tv_mine_integral = (TextView) view.findViewById(R.id.tv_mine_integral);
        tv_mine_coupon = (TextView) view.findViewById(R.id.tv_mine_coupon);
        tv_mine_collect = (TextView) view.findViewById(R.id.tv_mine_collect);
        tv_mine_share = (TextView) view.findViewById(R.id.tv_mine_share);
        rl_my_order = (RelativeLayout) view.findViewById(R.id.rl_my_order);
        ll_payment = (LinearLayout) view.findViewById(R.id.ll_payment);
        tv_wait_pay_num = (TextView) view.findViewById(R.id.tv_wait_pay_num);
        ll_shipped = (LinearLayout) view.findViewById(R.id.ll_shipped);
        tv_wait_delivery_num = (TextView) view.findViewById(R.id.tv_wait_delivery_num);
        ll_receipt = (LinearLayout) view.findViewById(R.id.ll_receipt);
        tv_wait_receipt_num = (TextView) view.findViewById(R.id.tv_wait_receipt_num);
        ll_evaluated = (LinearLayout) view.findViewById(R.id.ll_evaluated);
        tv_wait_comment_num = (TextView) view.findViewById(R.id.tv_wait_comment_num);
        ll_return = (LinearLayout) view.findViewById(R.id.ll_return);
        tv_wait_return_num = (TextView) view.findViewById(R.id.tv_wait_return_num);
        tv_mine_Business_info = (TextView) view.findViewById(R.id.tv_mine_Business_info);
        tv_mine_shop = (TextView) view.findViewById(R.id.tv_mine_shop);
        tv_mine_address = (TextView) view.findViewById(R.id.tv_mine_address);
        tv_mine_settings = (TextView) view.findViewById(R.id.tv_mine_settings);
    }

    private void setListener() {
        ll_right_sign.setOnClickListener(this);
        ll_msg.setOnClickListener(this);
        iv_mine_msg.setOnClickListener(this);
        iv_icon.setOnClickListener(this);
        iv_vip.setOnClickListener(this);
        tv_name.setOnClickListener(this);
        tv_info.setOnClickListener(this);
        tv_mine_integral.setOnClickListener(this);
        tv_mine_coupon.setOnClickListener(this);
        tv_mine_collect.setOnClickListener(this);
        tv_mine_share.setOnClickListener(this);
        rl_my_order.setOnClickListener(this);
        ll_payment.setOnClickListener(this);
        tv_wait_pay_num.setOnClickListener(this);
        ll_shipped.setOnClickListener(this);
        tv_wait_delivery_num.setOnClickListener(this);
        ll_receipt.setOnClickListener(this);
        tv_wait_receipt_num.setOnClickListener(this);
        ll_evaluated.setOnClickListener(this);
        tv_wait_comment_num.setOnClickListener(this);
        ll_return.setOnClickListener(this);
        tv_wait_return_num.setOnClickListener(this);
        tv_mine_Business_info.setOnClickListener(this);
        tv_mine_shop.setOnClickListener(this);
        tv_mine_address.setOnClickListener(this);
        tv_mine_settings.setOnClickListener(this);


    }

    @Override
    public void daySignSuccess(SignModel signModel) {
        if (signModel.getStatus() == HttpUtil.RESULT_OK) {
            SignModel.DataBean item = signModel.getData();
            String data = item.getTotal_point();
            if (!Tools.isNull(data)) {
                // 更新积分
                if (user != null) {
                    if (!Tools.isNull(data)) {
                        user.setPoint(Integer.parseInt(data));
                    }
                    App.getInstance().saveObject(user, HttpUtil.USER_KEY);
                }
            }
            showSignDialog(1, signModel.getMsg());
        } else {
            if (!Tools.isNull(signModel.getMsg()) && signModel.getMsg().equals("今天已经签过了")) {
                showSignDialog(0, signModel.getMsg());
            } else {
                T.showToast(mContext, signModel.getMsg());
            }
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.arg1) {
            case 1: {
                // 成功
                T.showToast(mContext, "分享成功");
            }
            break;
            case 2: {
                // 失败
                T.showToast(mContext, "分享失败");
            }
            break;
            case 3: {
                // 取消
                T.showToast(mContext, "分享取消");
            }
            break;
        }
        return false;
    }

    @Override
    public void onCancel(Platform platform, int action) {
        // 取消
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 3;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void onComplete(Platform platform, int action,
                           HashMap<String, Object> arg2) {
        // 成功
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 1;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, this);

    }

    @Override
    public void onError(Platform platform, int action, Throwable t) {
        // 失敗
        // 打印错误信息,print the error msg
        t.printStackTrace();
        // 错误监听,handle the error msg
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 2;
        msg.arg2 = action;
        msg.obj = t;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }

    @Override
    public void memberInfoSuccess(UserModel userModel) {
        if (userModel != null && userModel.getStatus() == HttpUtil.RESULT_OK) {
            User u = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
            int product_type = 0;
            if (u != null) {
                product_type = u.getProduct_type();
            }
            User data = userModel.getData();
            data.setAuthToken(userModel.getAuthToken());
            data.setProduct_type(product_type);
            App.getInstance().saveObject(data, HttpUtil.USER_KEY);
            int wait_pay_num = data.getWait_pay_num();
            int wait_delivery_num = data.getWait_delivery_num();
            int wait_receipt_num = data.getWait_receipt_num();
            int wait_comment_num = data.getWait_comment_num();
            int wait_return_num = data.getRefund_num();
            // 待付款
            if (wait_pay_num > 0) {
                tv_wait_pay_num.setVisibility(View.VISIBLE);
                tv_wait_pay_num.setText(wait_pay_num + "");
            } else {
                tv_wait_pay_num.setVisibility(View.GONE);
            }
            // 待发货
            if (wait_delivery_num > 0) {
                tv_wait_delivery_num.setVisibility(View.VISIBLE);
                tv_wait_delivery_num.setText(wait_delivery_num + "");
            } else {
                tv_wait_delivery_num.setVisibility(View.GONE);
            }
            // 待收货
            if (wait_receipt_num > 0) {
                tv_wait_receipt_num.setVisibility(View.VISIBLE);
                tv_wait_receipt_num.setText(wait_receipt_num + "");
            } else {
                tv_wait_receipt_num.setVisibility(View.GONE);
            }
            // 待评价
            if (wait_comment_num > 0) {
                tv_wait_comment_num.setVisibility(View.VISIBLE);
                tv_wait_comment_num.setText(wait_comment_num + "");
            } else {
                tv_wait_comment_num.setVisibility(View.GONE);
            }

            // 退换货
            if (wait_return_num > 0) {
                tv_wait_return_num.setVisibility(View.VISIBLE);
                tv_wait_return_num.setText(wait_return_num + "");
            } else {
                tv_wait_return_num.setVisibility(View.GONE);
            }

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.tv_mine_integral:
                //我的积分
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    // 客服权限  只能访问我的商品和消息模块
                    startAct(MyPointsActivity.class);
                }
                break;

            case R.id.tv_mine_coupon:
                // 我的优惠券
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    startAct(MyCouponActivity.class);
                }
                break;
            case R.id.tv_mine_collect:
                // 收藏管理
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    startAct(MyCollectionActivity.class);
                }
                break;
            case R.id.tv_mine_address:
                //地址管理
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    bundle.putInt("address_type", 1);
                    startAct(AddressManagementActivity.class, bundle);
                }
                break;

            case R.id.tv_mine_share:
                //我的分享
                showShareDialog();

//                new SharePopupWindows(mContext,share_title,share_text,share_url,getView()); //把分享封装一下
                break;
            case R.id.tv_mine_shop:
                // 我的商品
                // 1. 2是运营可以进我的商品并操作 1是客服能进我的商品不能操作
                if (roles.size() > 1) {
                    // 组合权限
                    if (roles.contains(1) || roles.contains(2)) {
                        // 如果组合中包含 运营和客服都是可以操作我的商品
                        // 如果是运营权限
                        // 2为已申请未交金3为已申请已交金4为已通过5为未通过6.补缴保证金
                        int is_shoper = user.getIs_shoper();
                        int is_year_fee = user.getIs_year_fee();//是否交年费 0为未交，1为已交
                        user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
                        if (is_shoper == 2) {
                            T.showToast(mContext, "正在审核中~");
                        } else if (is_shoper == 4) {
                            //已通过未交金
                            T.showToast(mContext, "审核通过，请支付保证金~");
                            startAct(ShopInfoDetailActivity.class);
                        } else if (is_shoper == 1 || is_shoper == 5) {
                            startAct(ShopInfoT1Activity.class);
                        } else if (is_shoper == 3 || is_shoper == 6) {
                            // 已交金 已通过
                            if (is_year_fee == 1) {
                                startAct(ShopManagementActivity.class);
                            } else {
                                startAct(AnnualFeeActivity.class);
                            }
                        }
                    }

                } else {
                    if (roles.contains(0) || roles.contains(2) || roles.contains(1)) {
                        // 如果是运营权限
                        // 2为已申请未交金3为已申请已交金4为已通过5为未通过6.补缴保证金
                        int is_shoper = user.getIs_shoper();
                        int is_year_fee = user.getIs_year_fee();//是否交年费 0为未交，1为已交
                        user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
                        if (is_shoper == 2) {
                            T.showToast(mContext, "正在审核中~");
                        } else if (is_shoper == 4) {
                            //已通过未交金
                            T.showToast(mContext, "审核通过，请支付保证金~");
                            startAct(ShopInfoDetailActivity.class);
                        } else if (is_shoper == 1 || is_shoper == 5) {
                            startAct(ShopInfoT1Activity.class);
                        } else if (is_shoper == 3 || is_shoper == 6) {
                            // 已交金 已通过
                            if (is_year_fee == 1) {
                                startAct(ShopManagementActivity.class);
                            } else {
                                startAct(AnnualFeeActivity.class);
                            }
                        }
                    } else {
                        T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                    }
                }
                break;
            case R.id.tv_mine_Business_info:
                if (roles.size() > 1) {
                    // 组合权限中如果有财务 才能进
                    if (roles.contains(3)) {
                        //商家信息 is_shoper是否为商户  1为否2为已申请未交金3为已申请已交金4为已通过5为未通过
                        user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
                        if (user != null) {
                            int is_shoper = user.getIs_shoper();
                            switch (is_shoper) {
                                case 1:
                                    startAct(ShopInfoT1Activity.class);
                                    break;
                                case 2:
                                case 3:
                                case 5:
                                case 4:
                                    // 已通过
                                    if (is_shoper == 4) {
                                        //已通过未交金
                                        T.showToast(mContext, "审核通过，请支付保证金~");
                                    }
                                    startAct(ShopInfoDetailActivity.class);
                                    break;
                            }
                        }
                    } else {
                        T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                    }
                } else {
                    if (roles.contains(1) || roles.contains(2)) {
                        T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                    } else {
                        //商家信息 is_shoper是否为商户  1为否2为已申请未交金3为已申请已交金4为已通过5为未通过
                        user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
                        if (user != null) {
                            int is_shoper = user.getIs_shoper();
                            switch (is_shoper) {
                                case 1:
                                    startAct(ShopInfoT1Activity.class);
                                    break;
                                case 2:
                                case 3:
                                case 5:
                                case 4:
                                    // 已通过
                                    if (is_shoper == 4) {
                                        //已通过未交金
                                        T.showToast(mContext, "审核通过，请支付保证金~");
                                    }
                                    startAct(ShopInfoDetailActivity.class);
                                    break;
                            }
                        }
                    }
                }
                break;
            case R.id.tv_mine_settings:
                // 设置
                startAct(SettingActivity.class);
                break;

            case R.id.ll_payment:
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    bundle.putInt("type", 1);
                    bundle.putString("status", "0");
                    startAct(MyOrderActivity.class, bundle);
                }
                break;
            case R.id.ll_shipped:
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    bundle.putInt("type", 2);
                    bundle.putString("status", "1");
                    startAct(MyOrderActivity.class, bundle);
                }
                break;
            case R.id.ll_receipt:
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    bundle.putInt("type", 3);
                    bundle.putString("status", "2");
                    startAct(MyOrderActivity.class, bundle);
                }
                break;
            case R.id.ll_evaluated:
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    bundle.putInt("type", 4);
                    bundle.putString("status", "3");
                    startAct(MyOrderActivity.class, bundle);
                }
                break;
            case R.id.ll_msg:
                // 运营不能访问聊天
                if (roles.size() > 1) {
                    if (roles.contains(1)) {
                        startAct(MessageActivity.class);
                    } else {
                        T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                    }
                } else {
                    if (roles.contains(2) || roles.contains(3)) {
                        T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                    } else {
                        startAct(MessageActivity.class);
                    }
                }
                break;
            case R.id.ll_right_sign:
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    showProgressDialog("正在签到，请稍候……");
                    daySignModel = new DaySignPresenter(this, mContext);
                    daySignModel.daySign();
                }
                break;
            case R.id.rl_my_order:
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    bundle.putInt("type", 0);
                    bundle.putString("status", "");
                    startAct(MyOrderActivity.class, bundle);
                }
                break;
            case R.id.tv_info:
                if (roles.contains(0)) {
                    startAct(PersonalActivity.class);
                } else {
                    T.showAnimToast(mContext, "子帐号不能编辑信息~");
                }
                break;
            case R.id.ll_return:
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    startAct(ReturnActivity.class);
                }
                break;
        }
    }
}
